import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:leader_help/utils/util.dart';
import 'package:get/get.dart';
import '../controller.dart';

class HomeHeadlinesView extends GetView<HomeController> {
  const HomeHeadlinesView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeController>(
      builder: (_) => Container(
        padding: EdgeInsets.all(10.r),
        margin: EdgeInsets.symmetric(horizontal: 10.r),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.r),
        ),
        child: Column(
          children: [
            CarouselSlider.builder(
              itemCount: controller.state.headlinesBanner.length,
              itemBuilder: (ctx, index, realIndex) {
                var _item = controller.state.headlinesBanner[index];
                return GestureDetector(
                  onTap: () {
                    controller.actionHeadlinesDetail();
                  },
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(10.r),
                    child: loadImage(url: _item, fit: BoxFit.fill),
                  ),
                );
              },
              options: CarouselOptions(
                  autoPlay: true,
                  disableCenter: true,
                  viewportFraction: 1,
                  height: 160.r
                  // scrollDirection: Axis.vertical,
                  ),
            ),
            SizedBox(
              height: 5.r,
            ),
            Row(
              children: [
                Expanded(
                  child: TabBar(
                      controller: controller.state.tabCtrl,
                      padding: EdgeInsets.zero,
                      isScrollable: true,
                      indicatorColor: ColorsUtil.red,
                      indicatorSize: TabBarIndicatorSize.label,
                      labelColor: ColorsUtil.red,
                      unselectedLabelColor: ColorsUtil.title,
                      labelStyle: TextStyle(
                          color: ColorsUtil.red,
                          fontSize: 14.sp,
                          fontWeight: FontWeight.bold),
                      unselectedLabelStyle:
                          TextStyle(color: ColorsUtil.title, fontSize: 14.sp),
                      tabs: controller.state.headlinesTitles
                          .asMap()
                          .map(
                            (key, value) => MapEntry(
                              key,
                              Tab(
                                child: Text(value),
                                height: 30.r,
                              ),
                            ),
                          )
                          .values
                          .toList()),
                ),
                SizedBox(
                  width: 10.r,
                ),
                InkWell(
                  onTap: controller.actionHeadlinesMore,
                  child: Row(
                    children: [
                      Text(
                        '更多',
                        style: pfMediumStyle(
                            color: ColorsUtil.content, fontSize: 13.sp),
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        size: 13.sp,
                        color: ColorsUtil.content,
                      )
                    ],
                  ),
                ),
              ],
            ),
            HeadlinesText(
              iden: '习近平',
              content:
                  '明年经济工作要[稳字当头、稳中求进]，各地区各部门要担负起稳定宏观经济的责任，各方面要积极推出有利于经济稳定的政策，政策发力适当靠前。',
              summary: '--2021年12月8日至10日中央经济工作会议上的要求',
              onTap: controller.actionHeadlinesDetail,
            ),
            controller.state.tabCtrl.index == 0
                ? HeadlinesText(
                    iden: '本省动态',
                    content: '落实黄河流域生态保护和高质量发展重大国家战略[郑州发布总体规划和起步区建设方案]',
                    summary: '--2021年12月08日08:16',
                    onTap: controller.actionHeadlinesDetail,
                  )
                : HeadlinesText(
                    iden: '刘鹤',
                    content:
                        '刘鹤指出，[数据正在成为关键生产要素。]要研究推进数据确权和分类分级管理，畅通数据交易流动，实现数据要素市场化配置，合理分配数据要素收益。',
                    summary: '--2021年11月20日刘鹤向2021中国5G+工业互联网大会开幕式致辞',
                    onTap: controller.actionHeadlinesDetail,
                  )
          ],
        ),
      ),
    );
  }
}

TextSpan getRichText({
  required String text,
  double? fontSize,
  Color? color,
  Color? richColor,
}) {
  final List<InlineSpan> texts = [];
  InlineSpan _getNormalSpan(String str) {
    return TextSpan(
      text: str,
      style: pfMediumStyle(
          color: color ?? ColorsUtil.title, fontSize: fontSize ?? 14.sp),
    );
  }

  InlineSpan _getRichSpan(String str) {
    return TextSpan(
      text: str,
      style: pfMediumStyle(
          color: richColor ?? ColorsUtil.red, fontSize: fontSize ?? 14.sp),
    );
  }

  void _getTexts(String str) {
    var t1 = str.indexOf('[');
    if (t1 != -1) {
      texts.add(
        _getNormalSpan(
          str.substring(0, t1),
        ),
      );
      if (t1 >= str.length) {
        return;
      }
      var _str = str.substring(t1 + 1);
      var t2 = _str.indexOf(']');
      if (t2 != -1) {
        texts.add(_getRichSpan(_str.substring(0, t2)));
        if (t2 >= _str.length) {
          return;
        }
        _getTexts(_str.substring(t2 + 1));
      } else {
        texts.add(_getNormalSpan(_str));
      }
    } else {
      texts.add(_getNormalSpan(str));
    }
  }

  _getTexts(text);
  return TextSpan(children: texts);
}

class TPText extends StatelessWidget {
  TPText({
    Key? key,
    required this.text,
    this.fontSize,
    this.color,
    this.richColor,
  }) : super(key: key);
  final String text;
  final double? fontSize;
  final Color? color;
  final Color? richColor;
  final List<InlineSpan> texts = [];
  @override
  Widget build(BuildContext context) {
    return RichText(
        text: getRichText(
      text: text,
      fontSize: fontSize,
      color: color,
      richColor: richColor,
    ));
  }
}

class HeadlinesText extends StatelessWidget {
  const HeadlinesText({
    Key? key,
    required this.iden,
    required this.content,
    required this.summary,
    required this.onTap,
  }) : super(key: key);
  final String iden;
  final String content;
  final String summary;
  final VoidCallback onTap;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        alignment: Alignment.centerLeft,
        margin: EdgeInsets.only(top: 10.r),
        padding: EdgeInsets.only(bottom: 8.r),
        decoration: const BoxDecoration(
            border: Border(bottom: BorderSide(color: ColorsUtil.separator))),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text.rich(
              TextSpan(children: [
                WidgetSpan(
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 5.r),
                    margin: EdgeInsets.only(right: 10.r),
                    decoration: BoxDecoration(
                      border: Border.all(color: ColorsUtil.orange, width: 1),
                    ),
                    child: Text(
                      iden,
                      style: pfMediumStyle(
                          color: ColorsUtil.orange, fontSize: 12.sp),
                    ),
                  ),
                ),
                // TextSpan(
                //   text: content,
                //   style:
                //       pfMediumStyle(color: ColorsUtil.title, fontSize: 14.sp),
                // )
                getRichText(text: content),
              ]),
            ),
            SizedBox(
              height: 8.r,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Flexible(
                  child: Text(
                    summary,
                    textAlign: TextAlign.end,
                    style: pfMediumStyle(
                        color: ColorsUtil.subTitle, fontSize: 12.sp),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}
